/*!
 * Copyright (c) 2023-present StarDreamNet
 */
(document.loader || (document.loader = [])).push(["user", class {
    constructor() {
        this.id = location.pathname.split("user/")[1];;
    };
    initUserPosts() {
        var da = {
            user_id: this.id,
        };

        $.post('/api/shequ/get_posts', da, (data) => {
            var d = data.data;
            var cards = $('.app-user-postlists');
            // this.loadPostsByData(d, cards); // 暂时搁置
            this.resize();

            $("#usergoods").text(`获赞：${d.map(v=>v.goods).reduce((a,b)=>a+b)}`)
            d.forEach((item) => {
                var postlist = $(`<div class="app-user-postlist"></div>`);
                var info = $(`<div class="app-user-postlist-info"><h2></h2><span></span></div>`);
                info.find("h2").text(item.title);
                info.find("span").text(`浏览：${item.views} 点赞：${item.goods}`);
                postlist.append(info);
                cards.append(postlist);
                postlist.on("click", function () {
                    window.open("/post/" + item.id);
                });
                this.resize();
            });
            
        });
    };
    pageSystem() {
        var tabs = $(".app-user-main-tab");
        var pages = $(".app-user-main-page>div");
        var tabblock = $(".app-user-main-tabblock");
        var th = this;

        tabs.each((i, v) => {
            v = $(v)
            v.on("click", function () {
                tabs.each((_, v) => $(v).removeClass("active"));
                pages.each((_, v) => $(v).css("display", "none"));

                var pos = v.position();
                tabblock.css("left", `${pos.left}px`)

                v.addClass("active");
                $(pages[i]).css('display', '');
                th.resize();
            })
        })

        $('#set_bcm').click(function () {
            var da = {
                identity: $('#identity').val(),
                password: $('#pw').val(),
            };

            $.post('/api/set_bcm', da, (data) => {
                if (data.code == 200) {
                    swal("绑定成功", "", "success");
                } else {
                    swal("绑定失败", data.msg, "error");
                }

            });
        });
        $('#send_set').click(function () {
            var da = {
                name: $('#set-name').val(),
                img: $('#set-img').val(),
            };

            $.post('/api/set_info', da, (data) => {
                if (data.code == 200) {
                    swal("设置成功", "", "success");
                } else {
                    swal("设置失败", data.msg, "error");
                }

            });
        });

        document.getElementById('logout').onclick = () => {
            var myRequest = new Request('/api/logout');
            fetch(myRequest)
                .then(function (response) {
                    alert('已退出登录')
                    location.href = '/'
                })
        }
    }
    init_(require) {
        const { resize, dateSpanString } = require("index");
        const { loadPostsByData } = require("postlist");

        this.resize = resize;
        this.dateSpanString = dateSpanString;
        this.loadPostsByData = loadPostsByData;
        this.initUserPosts();
        this.pageSystem();
    }
}])